<?php
/**
 * 课程控制器
 *
 * @package    app
 * @version    1.0
 * @author     Ray 33705910@qq.com
 * @license    MIT License
 * @copyright  2013 - 2015 Ray
 * @link       http://wangxiaolei.cn
 */

/**
 * 本控制器主要用于：
 * 1.
 * @package  app
 * @extends  Controller
 */
namespace admin;

class Controller_Course extends \Controller_BaseController {

	public $template = 'edu/template';

	public function action_index(){
		$params = array(
  			'title' => '课程管理',
  			'menu' => ''
  		);

		$params['terms'] = \Model_Category::find(13);
		if($params['terms']){
			$params['terms'] = $params['terms']->children()->get();
		}
		
  		$params['grades'] = \Model_Grade::getItems();

		$params['items'] = \Model_Course::getItems('*', null, array('grade', 'term'));

		if(\Input::is_ajax()){
			$items = array();
			foreach ($params['items'] as $key => $value) {
				$item = $value->to_array();
				$item['term'] = $value->term->to_array();
				$item['grade'] = $value->grade->to_array();
				array_push($items, $item);
			}

			$grades = array();
			foreach ($params['grades'] as $key => $value) {
				array_push($grades, $value->to_array());
			}
			die(json_encode(array('status' => 'succ', 'msg' => '', 'errcode' => 0, 'data' => $items, 'grades' => $grades)));
		}

  		\View::set_global($params);
		$this->template->content = \View::forge("edu/course/courses");
	}

	/**
	* 查看一个课程
	*
	* @return
	*/
	public function action_view($id){
		if( ! $id){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 10)));
			}
		}
		$course = \Model_Course::find($id);
		if( ! $course){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 11)));
			}
		}

		if(\Input::is_ajax()){
			die(json_encode(array('status' => 'succ', 'msg' => '', 'errcode' => 0, 'data' => $course->to_array())));
		}
	}

	/**
	* 新增一个课程
	*
	* @return
	*/
	public function action_create(){
		if(\Input::method() == 'POST'){

			$data = \Input::post();
			$data['is_delete'] = 0;

			$course = \Model_Course::forge($data);

			if($course->save()){
				\Response::redirect('/admin/course');
			}
		}
	}

	/**
	* 编辑一个课程
	*
	* @param $id int 课程ID
	* @return
	*/
	public function action_edit($id){
		if( ! $id){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 10)));
			}
		}
		$course = \Model_Course::find($id);
		if( ! $course){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 11)));
			}
		}

		$data = \Input::post();
		$data['is_discount'] = \Input::post('is_discount', 0);
		$course->set($data);
		if($course->save()){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'succ', 'msg' => '', 'errcode' => 0)));
			}
			\Response::redirect("/admin/course");
		}else{
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '操作失败', 'errcode' => 10)));
			}
		}
	}

	/**
	* 删除一个课程
	*
	* @param $id int 课程ID
	* @return
	*/
	public function action_delete($id){
		if( ! $id){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 10)));
			}
		}
		$course = \Model_Course::find($id);
		if( ! $course){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 11)));
			}
		}

		$course->is_delete = 1;
		if($course->save()){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'succ', 'msg' => '', 'errcode' => 0)));
			}
		}else{
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '操作失败', 'errcode' => 10)));
			}
		}
	}

	/**
	* 学期列表
	*
	*/
	public function action_terms(){
		$params['items'] = \Model_Term::getItems();

		if(\Input::is_ajax()){
			$items = array();
			foreach ($params['items'] as $key => $value) {
				array_push($items, $value->to_array());
			}
			die(json_encode(array('status' => 'succ', 'msg' => '', 'errcode' => 0, 'data' => $items)));
		}
	}

}
